/**
 * Copyright (c) 2014, 2016 itemis AG (http://www.itemis.eu) and others.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 * 
 * SPDX-License-Identifier: EPL-2.0
 */
package org.eclipse.xtend.core.tests.compiler;

import org.eclipse.xtend2.lib.StringConcatenation;
import org.junit.Test;

/**
 * @author Sebastian Zarnekow - Initial contribution and API
 */
@SuppressWarnings("all")
public class CompilerBug441580Test extends AbstractXtendCompilerTest {
  @Test
  public void test_01() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import java.io.InputStream");
    _builder.newLine();
    _builder.append("import java.io.ByteArrayOutputStream");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor");
    _builder.newLine();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def loadSample(String fileName) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("var InputStream in = null");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("var ByteArrayOutputStream out = null");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("try {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("in = class.classLoader.getResourceAsStream(fileName)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("out = new ByteArrayOutputStream");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("var byte[] buffer = newByteArrayOfSize(2048)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("var int size  ");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("while((size = in.read(buffer)) != -1) {");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("out.write(buffer, 0, size)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("out.flush");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("return  out.toByteArray");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("} finally {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("in?.close");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("out?.close");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("import java.io.ByteArrayOutputStream;");
    _builder_1.newLine();
    _builder_1.append("import java.io.InputStream;");
    _builder_1.newLine();
    _builder_1.append("import org.eclipse.xtext.xbase.lib.Exceptions;");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@SuppressWarnings(\"all\")");
    _builder_1.newLine();
    _builder_1.append("public class C {");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public byte[] loadSample(final String fileName) {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("try {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("InputStream in = null;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("ByteArrayOutputStream out = null;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("try {");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("in = this.getClass().getClassLoader().getResourceAsStream(fileName);");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("ByteArrayOutputStream _byteArrayOutputStream = new ByteArrayOutputStream();");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("out = _byteArrayOutputStream;");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("byte[] buffer = new byte[2048];");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("int size = 0;");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("while (((size = in.read(buffer)) != (-1))) {");
    _builder_1.newLine();
    _builder_1.append("          ");
    _builder_1.append("out.write(buffer, 0, size);");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("out.flush();");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("return out.toByteArray();");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("} finally {");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("if (in!=null) {");
    _builder_1.newLine();
    _builder_1.append("          ");
    _builder_1.append("in.close();");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("if (out!=null) {");
    _builder_1.newLine();
    _builder_1.append("          ");
    _builder_1.append("out.close();");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("} catch (Throwable _e) {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("throw Exceptions.sneakyThrow(_e);");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.assertCompilesTo(_builder, _builder_1);
  }

  @Test
  public void test_02() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def m() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("var int i = 0");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if ((i = i+1) < 10) {}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("@SuppressWarnings(\"all\")");
    _builder_1.newLine();
    _builder_1.append("public class C {");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public Object m() {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("Object _xblockexpression = null;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("{");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("int i = 0;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("Object _xifexpression = null;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("if (((i = (i + 1)) < 10)) {");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_xifexpression = null;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("_xblockexpression = _xifexpression;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("return _xblockexpression;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.assertCompilesTo(_builder, _builder_1);
  }

  @Test
  public void test_03() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def m() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("var int i = 0");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if ((i = (i=i+1)) < (i=(i+1))) {}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("@SuppressWarnings(\"all\")");
    _builder_1.newLine();
    _builder_1.append("public class C {");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public Object m() {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("Object _xblockexpression = null;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("{");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("int i = 0;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("Object _xifexpression = null;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("if (((i = (i = (i + 1))) < (i = (i + 1)))) {");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_xifexpression = null;");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("_xblockexpression = _xifexpression;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("return _xblockexpression;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.assertCompilesTo(_builder, _builder_1);
  }
}
